import React, { PureComponent } from 'react'
import store from '../store'
import { SUB_NUMBER } from '../store/count/constans'

export class Profile extends PureComponent {
    constructor(props) {
        super(props)
        this.state = {
          count: store.getState().count.count
        }
      }
      componentDidMount() {
        store.subscribe(() => {
          this.setState({
            count: store.getState().count.count
          })
        })
      }
      subNumber(number) {
        store.dispatch({
          type: SUB_NUMBER,
          number
        })
      }
  render() {
    const { count } = this.state
    return (
      <div>
        <h1>profile count: {count}</h1>
        <button onClick={()=>{this.subNumber(1)}}>-1</button>
        <button onClick={()=>{this.subNumber(5)}}>-5</button>
        <button onClick={()=>{this.subNumber(8)}}>-8</button>
      </div>
    )
  }
}

export default Profile
